package com.lx.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lx.entity.LxEqu;
import com.lx.entity.LxPar;
import com.lx.entity.LxUser;
import com.lx.entity.vo.LxParVo;
import com.lx.service.impl.LxParServiceImpl;
import com.lx.service.impl.LxUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵敬轩
 * @since 2021-09-12
 */
@RestController
@RequestMapping("/lx-par")
public class LxParController {

    @Autowired
    private LxParServiceImpl lxParService;
    @CrossOrigin
    @GetMapping("/getAll")
    public String getAllUser(Integer page, Integer limit){
        Page<LxParVo> parVoPage = new Page<>(page,limit);
        lxParService.pageQuery(parVoPage);
        System.out.println("total"+parVoPage.getTotal());
        System.out.println("record"+parVoPage.getRecords());
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("code",0);
        resultData.put("msg",null);
        resultData.put("data",parVoPage.getRecords());
        return JSON.toJSONString(resultData);
    }

    @CrossOrigin
    @GetMapping("/delete")
    public String deletePar(String id){
        HashMap<String , Object> resultData = new HashMap<>();
        System.out.println(id);

        boolean deleteEquId = lxParService.removeById(id);
        if (deleteEquId)
            resultData.put("msg","删除成功");
        else
            resultData.put("msg","删除失败");
        return JSON.toJSONString(resultData);
    }


    @CrossOrigin
    @PostMapping("/save")
    public String updateProvider(@RequestBody LxPar lxPar){
        System.out.println(lxPar);
        HashMap<String, Object> resultDate = new HashMap<>();
        boolean updateEqu = lxParService.saveOrUpdate(lxPar);
        if (updateEqu)
            resultDate.put("msg","信息更新成功");
        else
            resultDate.put("msg","信息更新失败");
        return JSON.toJSONString(resultDate);
    }

}
